#!/bin/bash

#控制节点安装配置cinder-api、cinder-scheduler服务
#http://www.cnblogs.com/elvi/p/7736768.html
######################################################
#以下在controller节点安装配置

#创建Nova数据库、用户
mysql -h controller -u root -pinnobase -e"create database cinder;
grant all privileges on cinder.* to 'cinder'@'localhost' identified by 'cinder';
grant all privileges on cinder.* to 'cinder'@'%' identified by 'cinder';
flush privileges;"

# keystone创建cinder用户、服务、API
source ~/admin-openstack.sh
openstack user create --domain default --password=cinder cinder
openstack role add --project service --user cinder admin
openstack service create --name cinderv2   --description "OpenStack Block Storage" volumev2
openstack service create --name cinderv3   --description "OpenStack Block Storage" volumev3
openstack endpoint create --region RegionOne   volumev2 public http://controller:8776/v2/%\(project_id\)s
openstack endpoint create --region RegionOne   volumev2 internal http://controller:8776/v2/%\(project_id\)s
openstack endpoint create --region RegionOne   volumev2 admin http://controller:8776/v2/%\(project_id\)s
openstack endpoint create --region RegionOne   volumev3 public http://controller:8776/v3/%\(project_id\)s
openstack endpoint create --region RegionOne   volumev3 internal http://controller:8776/v3/%\(project_id\)s
openstack endpoint create --region RegionOne   volumev3 admin http://controller:8776/v3/%\(project_id\)s

######################################################
#安装Cinder
yum install openstack-cinder -y
yum install nfs-utils -y #NFS
cp /etc/cinder/cinder.conf{,.bak}

#配置
echo '
[DEFAULT]
osapi_volume_listen = controller1
osapi_volume_listen_port = 8776
auth_strategy = keystone
log_dir = /var/log/cinder
state_path = /var/lib/cinder
glance_api_servers = http://controller:9292
transport_url = rabbit://openstack:openstack@controller:5673

[database]
connection = mysql+pymysql://cinder:cinder@controller/cinder

[keystone_authtoken]
auth_uri = http://controller:5000
auth_url = http://controller:35357
memcached_servers = controller1:11211,controller2:11211,controller3:11211
auth_type = password
project_domain_name = default
user_domain_name = default
project_name = service
username = cinder
password = cinder

[oslo_concurrency]
lock_path = /var/lib/cinder/tmp
'>/etc/cinder/cinder.conf
#nova
echo '
[cinder]
os_region_name = RegionOne
'>>/etc/nova/nova.conf

#初始化数据
su -s /bin/sh -c "cinder-manage db sync" cinder
mysql -h controller -u cinder -pcinder -e "use cinder;show tables;" #检测

#haproxy高可用配置
echo '
#Cinder_API_cluster
listen Cinder_API_cluster
bind controller:8776
#balance source
option tcpka
option httpchk
option tcplog
server controller1 controller1:8776 check inter 2000 rise 2 fall 5
server controller2 controller2:8776 check inter 2000 rise 2 fall 5
server controller3 controller3:8776 check inter 2000 rise 2 fall 5
'>>/etc/haproxy/haproxy.cfg
systemctl restart haproxy.service
netstat -antp|grep 8776

#启动服务
systemctl restart openstack-nova-api.service
systemctl enable openstack-cinder-api.service openstack-cinder-scheduler.service
systemctl start openstack-cinder-api.service openstack-cinder-scheduler.service
netstat -antp|grep 8776 #cheack


##查看到存储节点@lvm、@nfs 且up状态说明配置成功
cinder service-list
# +------------------+-------------+------+---------+-------+
# | Binary           | Host        | Zone | Status  | State |
# +------------------+-------------+------+---------+-------+
# | cinder-volume    | cinder1@lvm | nova | enabled | up    |
# | cinder-volume    | cinder1@nfs | nova | enabled | up    |
# +------------------+-------------+------+---------+-------+

#openstack volume service list #cheack cinder server
#systemctl restart openstack-cinder-api  openstack-cinder-scheduler 
# cinder-manage service remove <binary> <host>
# cinder-manage service remove cinder-scheduler  cinder1

#创建云硬盘类型,关联volum
#LVM 
#(backend_name与配置文件名对应)
cinder type-create lvm
cinder type-key lvm set volume_backend_name=lvm01
#NFS
cinder type-create nfs
cinder type-key nfs set volume_backend_name=nfs01
#cheack
cinder extra-specs-list 
#cinder type-list
#cinder type-delete nfs

#创建云盘(容量单位G)
openstack volume create --size 1 --type lvm disk01 #lvm类型
openstack volume create --size 1 --type nfs disk02 #nfs类型
openstack volume list